/*********************************************-*- mode:c -*-
 * board_bbox132.h
 * this file contains all board specific definitions
 * not the cpu spcific ones!
 *
 *  Part of AVR-Netino - http://code.google.com/p/avr-netino/
 *
 * Copyright (c) 2015 Michael Maassen
 * 
 * 2011-01-04:	port to AVR-Net-IO by M.Maassen <mic.maassen@gmail.com>
 * 2012-02-14:  port to Carrera Black Box D132 w/ Arduino 1.0.6
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version, or
 * 
 * under the terms of the  Common Development and Distribution License (CDDL) 
 * version 1, see http://www.opensource.org/licenses/cddl1.php
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * $Id$
 *
 ************************************************************
 * you must define one of the folowing macros to 
 * use this file:
 *
 * Names for special pins or hardware constants:
 *   -  as const static:
 *      #define defConstant(N,V) const static uint8_t N = V;
 *   -  as enum element, inside: enum xxx { ... };
 *      #define defConstant(N,V) N = V,
 * defConstants		get all pin fuctions by names
 *
 * pinDef(p,b,t,f,u,i,...)	define a pin by port,bit,timer,function,usage,pcint...
 *   - all in 1 byte (for m168/m328/m32/m644(p)
 *     #define pinDef(p,b,t,...)	((((P##p)-1)<<5) | (b) | ((t)<<3))
 *   - all in 2 byte (for m1280/m2560
 *     #define pinDef(p,b,t,...)	((((P##p)-1)<<8) | (b) | ((t)<<3))
 *   - only port for digital_pin_to_port_PGM[]
 *     #define pinDef(p,...)	(P##p)
 *   - only bit mask for digital_pin_to_bit_mask_PGM[]
 *     #define pinDef(p,b,...)	_BV(b)
 *   - only timer for digital_pin_to_timer_PGM[]
 *     #define pinDef(p,b,t,...)	(t)
 *   - all in struct pin_map_t { } for (SdFat/Sd2PinMap.h)
 *     #define pinDef(p,b,t,...)	{&DDR##p, &PIN##p, &PORT##p, b},
 *
 * anaDef(a,p,c,...)		define an analog input by A#,Pin,Chan
 *   - A# in an enum
 *     #define anaDef(a,p,c,...) A##a = (p),
 *   - map input to adc channel
 *     #define anaDef(a,p,c,...)	(c),
 *   - man analog to digital pins
 *     #define anaDef(a,p,c,...)	(p),
 *
 * Arduino Defines (optional):
 *   - ArduinoDefs_Digital
 *   - ArduinoDefs_Analog
 *   - ArduinoDefs_PWM
 *   - ArduinoDefs_PCINT
 *   - ArduinoDefs_USB
 *   
 ***********************************************************
 * Board:	Carrera Black Box D132
 * On the Arduino board, digital pins are also used
 * for the analog output (software PWM).  Analog input
 * pins are a separate set.

 * ATMEL ATMEGA16
 *
 *                                  I         A A A A
 *                                  N         D D D D
 *                              S   T         C C C C
 *                              S   2         0 1 2 3
 *
 *                              P P P P P G V P P P P
 *                              B B B B B N c A A A A
 *                              4 3 2 1 0 D c 0 1 2 3
 *
 *                              4 4 4 4 4 3 3 3 3 3 3
 *                              4 3 2 1 0 9 8 7 6 5 4
 *                            --+-+-+-+-+-+-+-+-+-+-+--
 *              (MOSI) PB5  1| *                       |33 PA4 (ADC4)
 *              (MISO) PB6  2|                         |32 PA5 (ADC5)
 *               (SCK) PB7  3|                         |31 PA6 (ADC6)
 *                     RST  4|        ATmega16         |30 PA7 (ADC7)
 *                     Vcc  5|                         |29 ARef
 *                     GND  6|          TQFP           |28 GND
 *                   XTAL2  7|                         |27 AVcc
 *                   XTAL1  8|                         |26 PC7
 *               (RxD) PD0  9|                         |25 PC5
 *               (TxD) PD1 10|                         |24 PC5
 *              (INT0) PD2 11|                         |23 PC4
 *                            --+-+-+-+-+-+-+-+-+-+-+--
 *                              1 1 1 1 1 1 1 1 2 2 2
 *                              2 3 4 5 6 7 8 9 0 1 2
 *
 *                              P P P P P V G P P P P 
 *                              D D D D D c N C C C C 
 *                              3 4 5 6 7 c D 0 1 2 3
 *
 *
 * PB4..7 are Lane-Change buttons
 * PD0..1 are PC-Unit 
 * PD2..3 Tower 1/2 receicer (or car id receiver)
 * PD4    is Rail output
 * PD5..7 PC0..1 are Modul connector
 * PD7    is speaker
 * PC2,3,7 PA4,5 are LEDs
 * PC6    is Pace Car button
 * PA7    is Rail read
 * PA6    is supply voltage
 * PA0..3 speed read out
 * PB0    is Fuel tank switch
 * PB1    is Start button
 * PB2    Lap Counter
 * PB3    Tower IR Led output
 ***********************************************************/

/***********************************************************
 * things we want allways 
 ***********************************************************/
// we define BLACK_BOX_D132 to flag different pinnig to libraries
// if set, we compile for Carrera BlackBox D132
#ifndef BLACK_BOX_D132
#define BLACK_BOX_D132	0x20150215	/* Date YYYYMMDD */
#endif

#ifdef pinDef
/***********************************************************
 * mapping from pin number to :
 * Port,Bit,Timer(PWM),spec.function,usage,pcint,...
 ***********************************************************/
/* ATmega with 4 Ports (m32/m644) = AVR-Net-IO */
/* 0 */
pinDef( B, 0 , NOT_ON_TIMER,T0,Fuel_SW,8)
pinDef( B, 1 , NOT_ON_TIMER,T1,StartKey,9)
pinDef( B, 2 , NOT_ON_TIMER,INT2,LapCount,10)
pinDef( B, 3 , TIMER0A,OC0,TowerOut,11)
#ifdef COM0B1
pinDef( B, 4 , TIMER0B,SS,LaneX1,12)
#else
pinDef( B, 4 , NOT_ON_TIMER,SS,LaneX1,12)
#endif
pinDef( B, 5 , NOT_ON_TIMER,MOSI,LaneX2,13)
pinDef( B, 6 , NOT_ON_TIMER,MISO,LaneX3,14)
pinDef( B, 7 , NOT_ON_TIMER,SCK,LaneX4,15)
/* 8 */
pinDef( C, 0 , NOT_ON_TIMER,SCL,ModulSCL,16)
pinDef( C, 1 , NOT_ON_TIMER,SDA,ModulSDA,17)
pinDef( C, 2 , NOT_ON_TIMER,TCK,LED4,18)
pinDef( C, 3 , NOT_ON_TIMER,TMS,LED5,19)
pinDef( C, 4 , NOT_ON_TIMER,TDO,NC1,20)
pinDef( C, 5 , NOT_ON_TIMER,TDI,NC2,21)
pinDef( C, 6 , NOT_ON_TIMER,TOSC1,PaceKey,22)
pinDef( C, 7 , NOT_ON_TIMER,TOSC2,LED3,23)
/* 16 */
pinDef( D, 0 , NOT_ON_TIMER,RXD,PC_RxD,24)
pinDef( D, 1 , NOT_ON_TIMER,TXD,PC_TxD,25)
pinDef( D, 2 , NOT_ON_TIMER,INT0,Tower1,26)
pinDef( D, 3 , NOT_ON_TIMER,INT1,CarID,27)
pinDef( D, 4 , TIMER1B,OC1B,Track,28)
pinDef( D, 5 , TIMER1A,OC1A,ModulA,29)
#ifdef COM21
pinDef( D, 6 , NOT_ON_TIMER,ICP1,ModulB,30)
pinDef( D, 7 , TIMER2,OC2,Buzzer,31)
#else
pinDef( D, 6 , TIMER2B,OC2B,ModulB,30)
pinDef( D, 7 , TIMER2A,OC2A,Buzzer,31)
#endif
/* 24 */
pinDef( A, 0 , NOT_ON_TIMER,ADC0,Speed1,0)
pinDef( A, 1 , NOT_ON_TIMER,ADC1,Speed2,1)
pinDef( A, 2 , NOT_ON_TIMER,ADC2,Speed3,2)
pinDef( A, 3 , NOT_ON_TIMER,ADC3,Speed4,3)
pinDef( A, 4 , NOT_ON_TIMER,ADC4,LED1,4)
pinDef( A, 5 , NOT_ON_TIMER,ADC5,LED2,5)
pinDef( A, 6 , NOT_ON_TIMER,ADC6,Uref,6)
pinDef( A, 7 , NOT_ON_TIMER,ADC7,Uout,7)
#endif /* pinDef */

#ifdef anaDef
/***********************************************************
 * mapping from analog pin to:
 * A*,digPin,adcChannel,
 ***********************************************************/
anaDef( 0, 24, 0)
anaDef( 1, 25, 1)
anaDef( 2, 26, 2)
anaDef( 3, 27, 3)
anaDef( 4, 28, 4)
anaDef( 5, 29, 5)
anaDef( 6, 30, 6)
anaDef( 7, 31, 7)
#endif	/* anaDef */

/***********************************************************
 * the following ArduinoDefs_* defines are optional
 * if defined these can lead to faster/smaller code
 ***********************************************************/
#if defined( ArduinoDefs_Digital )
#endif	/* ArduinoDefs_Digital */

#if defined( ArduinoDefs_Analog )
#define analogPinToChannel(P)		(P)
#define analogInputToDigitalPin(p)	(((p) < pins_count_analog) ? (p) + pins_ADC0 : -1)
#endif	/* ArduinoDefs_Analog */

#if defined( ArduinoDefs_PWM )
#if defined(COM21)
#if defined(COM0B1)
#define digitalPinHasPWM(p)         ((p) == 3 || (p) == 4 || (p) == 20 || (p) == 21 ||              (p) == 23)
#else
#define digitalPinHasPWM(p)         ((p) == 3 ||             (p) == 20 || (p) == 21 ||              (p) == 23)
#endif	/* COM0B1 */
#else
#if defined(COM0B1)
#define digitalPinHasPWM(p)         ((p) == 3 || (p) == 4 || (p) == 20 || (p) == 21 || (p) == 22 || (p) == 23)
#else
#define digitalPinHasPWM(p)         ((p) == 3 ||             (p) == 20 || (p) == 21 || (p) == 22 || (p) == 23)
#endif	/* COM0B1 */
#endif	/* COM21 */

#endif	/* ArduinoDefs_PWM */


#if defined( ArduinoDefs_USB )
#endif	/* ArduinoDefs_USB */

#if defined( ArduinoDefs_Serial )
#define SERIAL_PORT_MONITOR         Serial
#define SERIAL_PORT_HARDWARE        Serial
#if defined (UBRR1H)
#define SERIAL_PORT_HARDWARE1       Serial1
#define SERIAL_PORT_HARDWARE_OPEN   Serial1
#endif
#endif /* ArduinoDefs_Serial */

#ifdef defConstant
/***********************************************************
 * define constants for special pins
 ***********************************************************/
/* define board specific pin functions */

#ifndef defClass
#define defClass(C,N) C##_##N
#endif 


#if defined( All_Constants ) || defined( Arduino_Constants )
#endif


#if defined( All_Constants ) || defined( IRQ_Constants )
defConstant(defClass(CAR,INR) ,  2  )	// Int Nr of ENC28J60
defConstant(defClass(CONTROL,INR)	   ,  1  )	// Int Nr of RFM12
defConstant(defClass(LAPCOUNT,INR)	   ,  1  )	// Int Nr of RFM12
#endif

defConstant(defClass(_internal_,_avoid_empty_enum_),0)
#endif	/* defConstant */
/***********************************************************
 * clean up Xmacros
 ***********************************************************/
#undef pinDef
#undef anaDef
#undef defConstant
#undef defClass
#undef All_Constants
